package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.entity.mqtt.Mqunit;
import com.example.mapper.MqunitMapper;
import com.example.service.MqunitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MqunitServiceImpl extends ServiceImpl<MqunitMapper, Mqunit> implements MqunitService {
    @Resource
    private MqunitMapper mqunitMapper;
    @Override
    public Result<Map<String,Mqunit>> queryIPageList(Integer current,Integer size) {
        try{
            IPage<Mqunit> config = new Page<>(current, size);
            // 解决当搜索条件为空时，默认查询所有数据
            QueryWrapper<Mqunit> maintainQueryWrapper = new QueryWrapper<>();
            IPage<Mqunit> mqunitIPage = this.mqunitMapper.selectPage(config, maintainQueryWrapper);
            // 每页展示的数据
            List<Mqunit> mqunitList = mqunitIPage.getRecords();
            // 将响应数据填充到map中
            if (mqunitList.size()==0){
                return Result.error("暂无数据");
            }
            Map map = new HashMap();
            map.put("pages", mqunitIPage.getPages());
            map.put("total", mqunitIPage.getTotal());
            map.put("list", mqunitList);
            return Result.success(map);
        }catch (Exception e) {
            return Result.error("分页查询异常" + String.valueOf(e),"500");
        }
    }

    @Override
    public Result queryById(Long id) {
        try {
            Mqunit mqunit = this.mqunitMapper.selectById(id);
            if (mqunit==null){
                return Result.error("暂无数据","500");
            }
            return Result.success(mqunit);
        }catch (Exception e){
            return Result.error(String.valueOf(e+"选择的id不存在,发生异常"),"500");
        }
    }

    @Override
    public Mqunit queryByid(Long id) {
        try {
            Mqunit mqunit = this.mqunitMapper.selectById(id);
            return mqunit;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public  Result<List<Mqunit>> queryList() {
        try {
            List<Mqunit> mqunitList = this.mqunitMapper.selectList(null);
            if (mqunitList==null){
                return Result.error("暂无数据","500");
            }
            return Result.success(mqunitList);
        }catch (Exception e){
            return Result.error(String.valueOf("查询发生异常"+e),"500");
        }
    }
}
